﻿var CurrentUser = {};

Ext.onReady(function () {
    PageSafe();

    //地区树
    var AreaTree = new Ext.tree.TreePanel({
        width: 260,
        style: 'padding-left:20px',
        border: false,
        height: 300,
        split: true,
        rootVisible: true,
        useArrows: false, /*去掉树线*/
        autoScroll: true,
        collapseMode: 'mini',
        root: {},
        listeners: {
            render: function () {
                renderTree(AreaTree); /*渲染菜单树*/
            },
            checkchange: function (node, state) {
                if (state) {
                    //                    CurrentArea = node.attributes;
                    //                    Ext.getCmp("AreaName1").setText(CurrentArea.text);
                    //                    Ext.getCmp("ChargeName").setText(CurrentArea.AreaChargePerson.UserName);
                    //                    Ext.getCmp("Remark").setText(CurrentArea.AreaRemark);
                }
            }
        }
    });

    var winpage = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '基本信息修改',
            iconCls: 'personinfo-icon',
            layout: 'form',
            bodyStyle: 'padding:50px',
            defaults: {
                xtype: 'textfield',
                bodyStyle: 'margion-top:50px'
            },
            labelAlign: 'right',
            items: [{
                width: 100,
                fieldLabel: '用户名',
                readOnly: true,
                id: 'UserName',
                name: 'UserName'
            }, {
                fieldLabel: '分成比例',
                width: 100,
                readOnly: true,
                id: 'UserInto',
                name: 'UserInto'
            }, {
                fieldLabel: '用户姓名',
                width: 160,
                id: 'RealName',
                name: 'RealName'
            }, {
                fieldLabel: '所在地址',
                width: 260,
                id: 'Address',
                xtype: 'combotree',
                allowUnLeafClick: false,
                passName: 'Address',
                treeHeight: 300,
                tree: AreaTree
            }, {
                fieldLabel: '所属角色',
                width: 100,
                readOnly: true,
                id: 'RoleName',
                name: 'RoleName'
            }, {
                fieldLabel: '所属公司',
                width: 260,
                id: 'Company',
                name: 'Company'
            }, {
                fieldLabel: '联系电话',
                width: 160,
                id: 'Tel',
                name: 'Tel'
            }, {
                fieldLabel: '电子邮箱',
                width: 160,
                id: 'EMail',
                name: 'EMail'
            }, {
                fieldLabel: '年龄',
                width: 50,
                id: 'Age',
                name: 'Age'
            }, {
                fieldLabel: '职位',
                width: 100,
                id: 'Job',
                readOnly: true,
                name: 'Job'
            }, {
                xtype: 'button',
                iconCls: 'save-icon',
                text: '保存基本信息',
                handler: function () {
                    if (CurrentUser.PID == null) { Ext.Msg.alert("提示信息", "当前用户信息绑定有误，请刷新页面"); return; }
                    var re = new JsonRequest();
                    re.params.UserInfo = {
                        RoleID:CurrentUser.RoleID,
                        PID:CurrentUser.PID,
                        UserAccount:Ext.getCmp("UserName").getValue(),
                        UserName:Ext.getCmp("RealName").getValue(),
                        Address: Ext.getCmp("Address").passField.value,
                        Company: Ext.getCmp("Company").getValue(),
                        Tel: Ext.getCmp("Tel").getValue(),
                        Email: Ext.getCmp("EMail").getValue(),
                        Age: Ext.getCmp("Age").getValue(),
                        Job:Ext.getCmp("Job").getValue(),
                        UserInto: Ext.getCmp("UserInto").getValue()
                };
                Ext.Ajax.request({
                    url: Ext.getClassUrl('BaseManager.PersonManager.PersonView', 'UpdatePerson'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (obj.ResponseObject != null) {
                            CurrentUser = obj.ResponseObject;
                            BindData(CurrentUser);
                            Ext.Msg.alert("提示信息", "用户基本信息修改成功");
                        }
                        else {
                            Ext.Msg.alert("提示信息", "用户基本信息修改失败，请联系管理员");
                        }
                    }
                });
            }
        }]
    }],
    listeners: {
        render: function () {
            var re = new JsonRequest();
            Ext.Ajax.request({
                url: Ext.getClassUrl('BaseManager.PersonManager.PersonView', 'GetCurrentUser'),
                jsonData: re,
                success: function (re, op) {
                    var obj = Ext.decode(re.responseText);
                    if (obj.ResponseObject != null) {
                        CurrentUser = obj.ResponseObject;
                        BindData(CurrentUser);
                    }
                }
            });
        }
    }
});
});

var BindData = function (ob) {
    Ext.getCmp("UserName").setValue(ob.UserAccount);
    Ext.getCmp("UserInto").setValue(ob.UserInto);
    Ext.getCmp("RealName").setValue(ob.UserName);
    Ext.getCmp("Address").setValue(ob.ChargeArea.AreaName);
    Ext.getCmp("Company").setValue(ob.Company);
    Ext.getCmp("Tel").setValue(ob.Tel);
    Ext.getCmp("EMail").setValue(ob.Email);
    Ext.getCmp("Age").setValue(ob.Age);
    Ext.getCmp("Job").setValue(ob.Job);
    Ext.getCmp("RoleName").setValue(ob.RoleName);
}

//初始化树
var renderTree = function (tree) {
    var re = new JsonRequest();
    re.category = CATEGORY.READONLY;
    re.params.param = {};
    Ext.Ajax.request({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AreaData'),
        jsonData: re,
        success: function (re, op) {
            var obj = Ext.decode(re.responseText).ResponseObject;
            var ns = {}, f = tree;
            ns.Children = obj;
            var r = new Ext.tree.TreeNode({
                id: '0',
                text: '全部地区'
            });
            var selectedNode = null, selectedId = f.selectedId;
            function appendChild(node, o) {
                if (node.PID == selectedId) {
                    selectedNode = node;
                }
                if (o.Children != null && o.Children.length > 0) {
                    for (var a = 0; a < o.Children.length; a++) {
                        var n = new Ext.tree.TreeNode({
                            id: o.Children[a].PID,
                            icon: '../images/menu/ui-text-area.png',
                            text: o.Children[a].AreaName,
                            parentId: o.Children[a].ParentID,
                            AreaRemark: o.Children[a].AreaRemark,
                            ChargeArea: o.Children[a].ChargeArea
                        });
                        node.appendChild(n);
                        appendChild(n, o.Children[a]);

                    }
                }
            }
            appendChild(r, ns);
            f.setRootNode(r);

            tree.registerNode(r); /*注册节点, 不然getNodeById(node.id)找不到这个节点*/
            tree.root.expand(true);
        }
    })
}

